package moltencore.game.bubble;

import spark.math.Random;

public enum Type {
	Red(0, "red"),
	Green(1, "green"),
	Blue(2, "blue"),
	Yellow(3, "yellow"),
	Violet(4, "violet"),
	Orange(5, "orange"),
	Black(6, "black"),
	White(7, "white"),
	Rainbow(8, "rainbow"),
	Blazing(9, "blazing"),
	Ghost(10, "stone"),
	Lightning(11, "lightning"),
	Anchor(12, "anchor");

	private byte type;
	private String typeName;
	private static final int COUNT = Anchor.type + 1;
	private static final int ORDINARY_BUBBLE_START_INDEX = Red.type;
	private static final int ORDINARY_BUBBLE_END_INDEX = White.type;
	public static final int ORDINARY_BUBBLE_COUNT = ORDINARY_BUBBLE_END_INDEX - ORDINARY_BUBBLE_START_INDEX + 1;
	private static Type[] types = new Type[COUNT];
	static {
			types[Red.type] = Red;
			types[Green.type] = Green;
			types[Blue.type] = Blue;
			types[Yellow.type] = Yellow;
			types[Violet.type] = Violet;
			types[Orange.type] = Orange;
			types[Black.type] = Black;
			types[White.type] = White;
			types[Rainbow.type] = Rainbow;
			types[Blazing.type] = Blazing;
			types[Ghost.type] = Ghost;
			types[Lightning.type] = Lightning;
			types[Anchor.type] = Anchor;
	}

	private Type(int type, String typeName) {
		this.type = (byte)type;
		this.typeName = typeName;
	}

	public boolean isOrdinary() {
		return (type >= ORDINARY_BUBBLE_START_INDEX) && (type <= ORDINARY_BUBBLE_END_INDEX);
	}

	public byte toByte() {
		return type;
	}

	public static Type fromByte(byte value) {
		return types[value];
	}

	static Type randomOrdinary(int differentOrdinaryBubblesCount) {
		int random = Random.getInstance().nextInt(differentOrdinaryBubblesCount);
		return types[ORDINARY_BUBBLE_START_INDEX + random];
	}

	@Override
	public String toString() {
		return typeName;
	}

	public static java.util.Iterator iterator() {
		return new Iterator();
	}

	public static class Iterator implements java.util.Iterator {
		private int i = 0;

		public boolean hasNext() {
			return (i < COUNT);
		}

		public Type next() {
			return types[i++];
		}

		public void remove() {
			throw new RuntimeException("Cant remove from this iterator");
		}
	}
}